package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmProcess;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmTask;
import com.harmontronics.hanma.paas.paas.dto.input.IdStrInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmProcessResult;
import com.harmontronics.hanma.paas.paas.service.HmProcessHistoryAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 吕大兵
 */
@RestController
@RequestMapping("/api/hm-process-history")
@Api(tags = {"流程执行历史"})
public class HmProcessHistoryController {
    @Autowired
    private HmProcessHistoryAppService hmProcessHistoryAppService;

    @GetMapping(value = "task/down")
    @ApiOperation(value = "已处理的任务")
    public ResponseEntity<PageBase<HmTask>> listDown(@RequestParam(required = false) Long processId, @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                     @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTask> pageBase = hmProcessHistoryAppService.listDown(processId, pageNo, pageSize);
        return ResponseEntity.ok(pageBase);
    }


    @GetMapping(value = "task/down/process")
    @ApiOperation(value = "已处理任务关联流程")
    public ResponseEntity<List<HmProcessResult>> listDownProcess() {
        List<HmProcess> processes = hmProcessHistoryAppService.getTaskDownInvolveProcess();
        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, processes));
    }


    @GetMapping(value = "process/cc")
    @ApiOperation(value = "存在抄送的流程")
    public ResponseEntity<List<HmProcessResult>> ccProcess() {
        List<HmProcess> processes = hmProcessHistoryAppService.getCCInvolveProcess();
        return ResponseEntity.ok(Convert.toList(HmProcessResult.class, processes));
    }

    @GetMapping(value = "task/cc")
    @ApiOperation(value = "我的知会任务")
    public ResponseEntity<PageBase<HmTask>> taskCC(@RequestParam(required = false) Long processId,
                                                   @RequestParam Boolean read,
                                                   @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
                                                   @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmTask> pageBase = hmProcessHistoryAppService.listTaskCC(processId, read,pageNo, pageSize);
        return ResponseEntity.ok(pageBase);
    }


    @PutMapping(value = "/read/cc")
    @ApiOperation("通知已读")
    public ResponseEntity<String> readCC(@RequestBody IdStrInput input){
        hmProcessHistoryAppService.readCC(input.getId());
        return ResponseEntity.ok();
    }


}
